package com.caiyi.financial.nirvana.sms.service;

import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsUserSign;
import com.caiyi.financial.nirvana.sms.enums.*;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsSignMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.SignQueryBean;
import com.youyu.common.api.Result;
import com.caiyi.financial.nirvana.sms.util.ValidateUtil;
import com.github.pagehelper.PageHelper;
import com.youyu.common.enums.BaseResultCode;
import com.youyu.common.exception.BizException;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SmsUserSignService extends BaseService<SmsUserSign> {


    @Autowired
    private SmsSignMapper smsSmsSignMapper;

    public int addSign(SmsUserSign smsSign) {

        YyAssert.isFalse(smsSign.getReason() != null && smsSign.getReason().length() > 50, ReturnCodeEnum.PARAMS_ERROR.code, "审核原因过长");
        YyAssert.isFalse(smsSign.getApplication() == null || (smsSign.getApplication() != SmsUserSignApplicationEnum.OTHER.index && smsSign.getApplication() != SmsUserSignApplicationEnum.MY.index && smsSign.getApplication() != SmsUserSignApplicationEnum.MYAPP.index), ReturnCodeEnum.PARAMS_ERROR.code, "应用场景只能设置为为自己公司、为自己产品、为他人产品");
        YyAssert.isFalse(smsSign.getIndustry() == null || (smsSign.getIndustry() != SmsUserSignIndustryEnum.BANKING.index && smsSign.getIndustry() != SmsUserSignIndustryEnum.GAME.index && smsSign.getIndustry() != SmsUserSignIndustryEnum.OTHER.index), ReturnCodeEnum.PARAMS_ERROR.code, "行业只能为网络游戏、互联网金融、其他");
        smsSign.setStatus(SmsUserSignStatusEnum.AUDIT.index);
        YyAssert.isFalse(!ValidateUtil.isNotNll(smsSign.getSign()) || !ValidateUtil.isNotNll(smsSign.getAccount()), ReturnCodeEnum.PARAMS_ERROR.code, "账户或签名不能为空");
        YyAssert.isFalse(smsSign.getSign().length() > 50, ReturnCodeEnum.PARAMS_ERROR.code, "签名过长");
        int insertCount = 0;
        //签名前后加上【】
        if (!smsSign.getSign().startsWith("【")) {
            smsSign.setSign("【" + smsSign.getSign());
        }
        if (!smsSign.getSign().endsWith("】")) {
            smsSign.setSign(smsSign.getSign() + "】");
        }
        List<String> list = smsSmsSignMapper.selectSignListByAccounts(smsSign.getAccount());
        if (list.contains(smsSign.getSign())) {
            YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "签名已存在");
        }
        smsSign.setDefauls(SmsUserSignDefaultEnum.DEFAULT.index);
        insertCount = this.insertByEntitySelective(smsSign);
        YyAssert.isFalse(insertCount == 0, ReturnCodeEnum.FAILED.code, "添加失败");
        return insertCount;
    }

    public int deleteSmsSign(Integer id, String account) {
        int deleteCount = 0;
        YyAssert.isFalse(id == null, ReturnCodeEnum.PARAMS_ERROR.code, "ID不能为空");
        SmsUserSign smsUserSign = new SmsUserSign();
        smsUserSign.setId(id);
        smsUserSign = this.selectByPrimaryKey(smsUserSign);
        deleteCount = smsSmsSignMapper.delByAccountAndId(account, id);
        YyAssert.isFalse(deleteCount == 0, ReturnCodeEnum.FAILED.code, "删除失败");
        logger.info("customer deleteSmsSign id {}, status is {}", id, smsUserSign.getStatus());
        return deleteCount;
    }

    /**
     * @param:defauls 默认为1，非默认为0
     * 该签名设置为默认需要更新用户签名列
     */
    public int updateSignDefault(Integer id, String account) {
        int updateCount = 0;
        YyAssert.isFalse(id == null, ReturnCodeEnum.PARAMS_ERROR.code, "ID不能为空");
        smsSmsSignMapper.updateSignDefault(account);
        updateCount = smsSmsSignMapper.updateSignByAccountAndId(id, account);
        YyAssert.isFalse(updateCount == 0, ReturnCodeEnum.FAILED.code, "更新默认失败");
        updateRedis(account);
        return updateCount;
    }

    public PageBean<SmsUserSign> getSignByParams(SignQueryBean bean) {
        PageBean<SmsUserSign> pageBean = null;
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsUserSign> list = smsSmsSignMapper.selectUserSignListByParams(bean);
        pageBean = new PageBean<>(list);
        return pageBean;
    }

    private void updateRedis(String account) {
        String key = String.format(JedisKey.SMS_SIGN, account);
        List<String> list = smsSmsSignMapper.selectSignListByAccount(account);
        Map<String, Double> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i), (double) list.size() - i - 1);
        }
        if (map.size() == 0) {
            logger.info("customer updateRedis account is {} sign is null", account);
            return;
        }
        JedisService.del(key);
        JedisService.zadd(key, map);
        logger.info("customer updateRedis account is {}, addSignCount is {} ", account, map.size());
    }
}
